/**
 * 6. Z 字形变换    
 */
class Solution2 {
    public String convert(String ss, int numRows) {
        if(numRows == 1) return ss;
        StringBuilder ret = new StringBuilder();
        char[] s = ss.toCharArray();
        // 公差和字符串长度
        int d = 2 * numRows - 2, len = s.length;
        for(int i = 0; i < numRows; i++) {
            int row = i, k = 0, cur = 0;
            while(cur < len) {
                if(row == 0 || row == (numRows - 1)) {
                    cur = row + k * d;
                    if(cur < len) {
                        ret.append(s[cur]);
                    }
                    k++;
                } else {
                    cur = row + k * d;
                    if(cur < len) {
                        ret.append(s[cur]);
                    }
                    cur = d - row + k * d;
                    if(cur < len) {
                        ret.append(s[cur]);
                    }
                    k++;
                }
            }
        }

        return new String(ret);
    }
}